package com.hq.fxh.No1_Infromation.Infor.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hq.fxh.No0_ChengQu.Adapter.PAdapter_Home;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No0_ChengQu.Fragment.HomeFragment;
import com.hq.fxh.Utils.TabUtils;
import com.hq.fxh.imageLoader.video.SelectVideoActivity;
import com.hq.fxh.No1_Infromation.Video.Fragment.VideoFragment;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * @author 薛
 * @ClassName
 * @Description :资讯框架页
 * @Created ${data}
 * @Email xlwenhao1858@sina.com
 */

public class InformationFragment extends BaseFragment implements View.OnClickListener {
     protected TabLayout mTabLayout;
    protected ViewPager inforViewPager;
    protected LinearLayout layoutInforFaTie;
    Fragment_infor inforFragment;
    VideoFragment videoFragment;
    HomeFragment mFragment3;
    Context mcontext;
    String[] titles = {"资讯", "视频", "精选"};
    private View view;
    private ArrayList<Fragment> fragments;


    public static InformationFragment newInstance() {

        Bundle args = new Bundle();

        InformationFragment fragment = new InformationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext = getActivity();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_infor;
    }

    @Override
    protected void findView(View rootView) {
        mTabLayout = (TabLayout) rootView.findViewById(R.id.infor_tabLayout);
        inforViewPager = (ViewPager) rootView.findViewById(R.id.infor_viewPager);
        layoutInforFaTie = (LinearLayout) rootView.findViewById(R.id.layout_Infor_FaTie);
        layoutInforFaTie.setOnClickListener(InformationFragment.this);
        inforViewPager.setPageMargin(3);
        inforViewPager.setOffscreenPageLimit(3);
        initTab();
        initListent();
    }

    @Override
    protected void initData() {

    }

    private void initListent() {
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {


                if (tab.getPosition() == 1) {
                    setHindForFatie(false);
                } else {
                    setHindForFatie(true);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //  changeTabStatus(tab, false);


            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    private void initTab() {


        inforFragment = new Fragment_infor();
        videoFragment = new VideoFragment();
        videoFragment.setInforContext(this);
        mFragment3 = new HomeFragment();
        mFragment3.initData('1', '9');
        if (fragments == null) {
            fragments = new ArrayList<>();
        } else {
            fragments.clear();
        }
        fragments.add(inforFragment);
        fragments.add(videoFragment);
        fragments.add(mFragment3);
        PAdapter_Home adapter = new PAdapter_Home(getChildFragmentManager(), fragments);
        inforViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(inforViewPager));

        inforViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.e("inforviewPager页数" + position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        inforViewPager.setAdapter(adapter);

       TabUtils. setIndicator(mTabLayout, 20, 20);
    }

    private void initView(View rootView) {

    }

    /*  调整tablayout中item的大小与位置*/

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.layout_Infor_FaTie) {
            Intent intent = new Intent(getContext(), SelectVideoActivity.class);
            startActivity(intent);
        }
    }

    public void setHindForFatie(boolean states) {
        if (states) {
            layoutInforFaTie.setVisibility(View.GONE);
        } else {
            layoutInforFaTie.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtils.e(hidden + " 控制显隐");
        if (hidden) {
        } else {

        }
    }
}
